<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class MapController extends Controller
{
    //地图数据查询
    public function index(){
    	$result = [];
    	$menu = Db::table('map')->where('is_display','=','1')->get();

    	foreach ($menu as $key => $value) {
    		$path_array = [];
    		$lat_lng = [];
    		$lat_lng[] = $value->lng;
    		$lat_lng[] = $value->lat;
    		$geoCoordMap[$value->city] = $lat_lng;

            $where = [];
            $where[] = ['map_id','=',$value->id];
            $where[] = ['is_display','=','1'];
    		$map_detail = Db::table('map_detail')->where($where)->get();

    		if(!empty(count($map_detail))){
    			$k = 0;
    			foreach ($map_detail as $key => $value_detail) {
                    $map = Db::table('map')->where('id','=',$value_detail->map_guide)->first();
                    if($map->is_display == '-1')continue;

    				$namespace['name'] = $value->city;
    				$path_array[$k][] = $namespace;

    				foreach ($menu as $menus) {
    					if($menus->id == $value_detail->map_guide){
    						$namespace_two = [];
    						$namespace_two['name'] = $menus->city;
    						$namespace_two['value'] = $value_detail->value;

    						$path_array[$k][] = $namespace_two;
    						break;
    					}
    				}
    				$k++;
    			}

    			$namespace_three = [];
    			$namespace_three[] = $value->city;
    			$namespace_three[] = $path_array;

                if(empty($path_array))continue;

    			$front[] = $namespace_three;
    		}
    	}

        if(empty($front))$front = [];
        if(empty($geoCoordMap))$geoCoordMap = [];

    	$result['geoCoordMap'] = $geoCoordMap;
    	$result['front'] = $front;

    	return response()->json(['code'=>'1','data'=>$result]);
    }
}
